/**   
 * 文件名：ReplyService.java</br>
 * 描述： </br>
 * 开发人员：汪浩博 </br>
 * 创建时间： 2016-5-20
 */ 

package com.post.service;

import com.base.util.Page;
import com.post.bean.Post;
import com.post.bean.Reply;
import com.post.dao.PostDAO;
import com.post.dao.ReplyDAO;

/** 
 * 类名: ReplyService</br> 
 * 包名：com.post.service </br> 
 * 描述: </br>
 * 发布版本号：</br>
 * 开发人员： 汪浩博</br>
 * 创建时间： 2016-5-20 
 */

public class ReplyService {

	private ReplyDAO replyDAO;
	private PostDAO postDAO;

	public ReplyDAO getReplyDAO() {
		return replyDAO;
	}
	public void setReplyDAO(ReplyDAO replyDAO) {
		this.replyDAO = replyDAO;
	}
	public PostDAO getPostDAO() {
		return postDAO;
	}
	public void setPostDAO(PostDAO postDAO) {
		this.postDAO = postDAO;
	}
	/**
	 * 方法名: getSinglePost</br>
	 * 详述: 通过id获取单个帖子</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-20</br>
	 * @param pid
	 * @return
	 */
	public Post getSinglePost(String pid){
		Post post = null;
		String hql = "from Post where id='"+pid+"'";
		try {
			post = (Post) postDAO.findByHql(hql).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return post;
	}
	
	public Page Querypage(Reply reply, int pagenum, int pagesize){
		String queryString = this.Sethql(reply);
		Page page = null;
		try {
			page = replyDAO.getPage(queryString, pagenum, pagesize);
		}catch (Exception e) {
			e.printStackTrace();	
		}	
		return page;	
	}
	
	public String Sethql(Reply reply){
		StringBuilder queryString = new StringBuilder("from Reply a");
		if(reply!=null){		
//			if (info.getUserName() != null&& !"".equals(info.getUserName())) {
//				queryString.append(" and lower(c.username) like '%").append(info.getUserName().toLowerCase()).append("%'");	
//			}
			if (reply.getPostId() != null && !"".equals(reply.getPostId())) {
				queryString.append(" where a.postId = '").append(reply.getPostId()).append("'");
			}
			queryString.append(" order by a.replyTime");
		}
		return queryString.toString();
	}
	/**
	 * 方法名: addReply</br>
	 * 详述: 新增或者更新已经存在的回复</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-20</br>
	 * @param reply
	 */
	public void addReply(Reply reply){
		try {
			replyDAO.saveOrUpdate(reply);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 方法名: findPageReplyByUid</br>
	 * 详述: 查询个人相关回复</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-21</br>
	 * @param uid 用户id
	 * @param pagenum
	 * @param pagesize
	 * @return
	 */ 
	public Page findPageReplyByUid(Long uid, Integer pagenum, Integer pagesize) {
		Page page = null;
		String queryString = "from Post where id in (select b.postId from Reply b where b.userId = '"+uid+"') and postStatus !='1'";
		page = replyDAO.getPage(queryString, pagenum, pagesize);
		return page;
	}
	
	
}
